<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- <script>
      const person = {
        name: "jack",
        age: 18,
      };

      const proxy = new Proxy(person, {
        get(target, key, receiver) {
          /*
            target  代理前的对象：person
            key     读取某个属性：name
            receiver 代理后的对象：proxy
          */
          console.log("proxy get");
          // console.log(target, key, receiver);
          // return target[key];
          return Reflect.get(target, key, receiver);
        },
        set(target, key, val, receiver) {
          /*
            target  代理前的对象：person
            key     设置某个属性：name
            val     设置属性的值
            receiver 代理后的对象：proxy
          */
          console.log("proxy set");
          // target[key] = val;
          Reflect.set(target, key, val, receiver);
        },
      });

      // console.log(proxy.name);
      proxy.sex = "男";

      console.log(proxy.sex);
    </script> -->

    <script>
      const person = {
        name: "jack",
        age: 18,
      };

      function defineReactive(target, key) {
        Object.defineProperty(target, key, {
          get() {
            console.log("get");
            return target[key];
          },
          set(val) {
            console.log("set");
            target[key] = val;
          },
        });
      }

      Object.keys(person).forEach((key) => defineReactive(person, key));
      
      person.sex = "男";

      console.log(person.sex);
    </script>
  </body>
</html>
